<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<%@ include file="../bui_header.jsp" %>

<body>

<div class="container">
    <div class="row">
      <form id="searchForm" class="form-horizontal span24">
        
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">公司名称：</label>
            <div class="controls">
              <input type="text" class="text" name="company_name">
            </div>
          </div>
         <!--  <div class="control-group span8">
            <label class="control-label">所属年度：</label>
            <div class="controls">
              <input type="text" class="text" name="belong_year">
            </div>
          </div> -->
          <div class="span4">
            <button type="button" id="btnSearch" class="button button-primary">搜索</button>
          </div>
        </div>
      </form>
    </div>
    <div class="search-grid-container">
      <div id="grid"></div>
    </div>

  </div>
  
  <div id="content" class="hide" disabled='true'>
      <form id="user_form" class="form-horizontal">
      	<div id="selectCompanyDiv" class="row">
      		<div class="control-group span8">
			    <label class="control-label">请选择公司</label>
			    <div class="controls">
			    	<div id="companySelect"></div>
				    <!-- <select name="companyNameSelect"></select> -->
			    </div>
		  	</div>
		</div>
        <div class="row">
          <div class="control-group span8" style="display:none">
            <label class="control-label"><s>*</s>编号</label>
            <div class="controls">
              <input name="number" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label"><s>*</s>企业名称</label>
            <div class="controls">
              <input name="companyName" type="text" data-rules="{required:true}" class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">所属年度</label>
            <div class="controls">
              <input name="belongYear" type="text" class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">放大倍数</label>
            <div class="controls">
              <input name="fdbs" type="text"  class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">放大倍数评分</label>
            <div class="controls">
              <input name=fdbspf type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">代偿及逾期率</label>
            <div class="controls">
              <input name="dcjyql" type="text"  class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">代偿及逾期率评分</label>
            <div class="controls">
              <input name="dcjyqlpf" type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">监管评级结果</label>
            <div class="controls">
              <input name="jdpjjg" type="text"  class="input-normal control-text">
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">监管评级结果评分</label>
            <div class="controls">
              <input name="jgpjjgpf" type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
          <div class="control-group span8">
            <label class="control-label">重大机构名称</label>
            <div class="controls">
            	<div id="zdjgmcSelect"></div>
              	<!-- <input name="zdjgmc" type="text"  class="input-normal control-text"> -->
            </div>
          </div>
          <div class="control-group span8">
            <label class="control-label">重大机构融资规模</label>
            <div class="controls">
              <input name="zdjgrzgm" type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
        <div class="row">
           <div class="control-group span8">
            <label class="control-label">重大机构评分</label>
            <div class="controls">
              <input name="zdjgpf" type="text"  class="input-normal control-text">
            </div>
          </div>
        </div>
      </form>
    </div>
    <div class="container hide">
	    <div class="row">
	    <div class="span24">
	      <div id="importFileDiv">
	      </div>
	    </div>
	 </div>
	 </div>
  
<script type="text/javascript">
BUI.use(['common/search', 'bui/overlay', 'bui/uploader', 'bui/form', 'bui/select', 'bui/data'], 
		function (Search, Overlay, Uploader, Form, Select, Data) {
    
    var editing = new BUI.Grid.Plugins.DialogEditing({
        contentId : 'content', //设置隐藏的Dialog内容
        autoSave : true,//添加数据或者修改数据时，自动保存
      });
    
    var columns = [
        /* {title:'编号',dataIndex:'number',width:60
        }, */
        {title:'企业名称', dataIndex:'companyName', width:'15%'},
        {title:'所属年度', dataIndex:'belongYear',width:'10%'},
        {title:'放大倍数',dataIndex:'fdbs',width:'10%'},
        {title:'放大倍数评分', dataIndex:'fdbspf',width:'10%'},
        {title:'代偿及逾期率',dataIndex:'dcjyql',width:'10%'}, 
        {title:'代偿及逾期率评分',dataIndex:'dcjyqlpf',width:'10%'}, 
        {title:'监管评级结果',dataIndex:'jdpjjg',width:'10%'}, 
        {title:'监管评级结果评分',dataIndex:'jgpjjgpf',width:'10%'}, 
        {title: '操作', width: '15%', renderer: function(value, obj) {
      	  var viewBtn = '<span class="grid-command btn-view">查看</span>&nbsp;&nbsp;';
      	  var editBtn = '<span class="grid-command btn-edit">编辑</span>';
      	  return viewBtn + editBtn;
        }}
      ];
      var store = Search.createStore('<%=contextPath %>/bizinfo/rzdbgs/getdata.do', {
    	  autoSync: true,
    	  proxy: {
      		method: "post",
      		dataType : 'json',
      		save : { //也可以是一个字符串，那么增删改，都会往那么路径提交数据，同时附加参数saveType
				addUrl : '<%=contextPath %>/bizinfo/rzdbgs/editData.do?action=add',
				updateUrl : '<%=contextPath %>/bizinfo/rzdbgs/editData.do?action=update',
				removeUrl : '<%=contextPath %>/bizinfo/rzdbgs/editData.do?action=remove'
         	}
    	  }
      });
      initSearchStore(store);
      
      var height = $(window).height() - 70;  
      var gridCfg = Search.createGridCfg(columns, {
    	  height: height,
    	  //width:'200%',
        tbar : {
          items : [
            {text : '<i class="icon-plus"></i>新建',btnCls : 'button button-small',handler: addFunction},
            {text : '<i class="icon-remove"></i>删除',btnCls : 'button button-small',handler : delFunction},
            {text : '<i class="icon-upload icon-white"></i>导入',btnCls : 'button button-small button-primary', handler: importData}
          ]
        },
        plugins : [BUI.Grid.Plugins.CheckSelection,editing] // 插件形式引入多选表格
      });

    var search = new Search({
        store : store,
        gridCfg : gridCfg
      });
    var grid = search.get('grid');
    
    initEditing(editing);
	ss();
    
    function ss() {
    	var $grid = $(".bui-grid");
    	if (!$grid)
  		  return ;
    	var $gridBody = $grid.find(".bui-grid-body");
    	if (!$gridBody)
  		  return ;
    	$gridBody.css('overflow-y', 'auto');
	  
	  var $gridTable = $grid.find(".bui-grid-table");
	  if ($gridTable)
		  $gridTable.width($gridBody.width()); 
    }
    // 新增操作
    function addFunction(){
        var newData = {isNew : true}; //标志是新增加的记录
        editing.add(newData); //添加记录后，直接编辑
        setCompanySelectVisible(true);
    }
    
    // 删除操作
    function delFunction(){
      var selections = grid.getSelection();
      delItems(selections);
    }
    // 导入操作
   function importData() {
    	var uploader = new Uploader.Uploader({
   	      	render: '#importFileDiv',
   	      	url: '<%=contextPath %>/bizinfo/rzdbgs/importData.do',
   	      	autoUpload: false,
   	      	multiple: false,
   	      	text: '选择导入文件',
   	     	width: 360,
   	     	rules: {
   	     	 // maxSize: [1024, '文件最大不能超过1M!'],
   	     	 // minSize: [1, '文件最小不能小于1k!'],
   	     	  max: [1, '文件最多不能超过{0}个！'],
   	     	 // min: [1, '文件最少不能少于{0}个!'],
   	     	  ext: ['.xls', '文件类型只能为{0}']
   	     	},
   	     	isSuccess: function(result) {
   	     		if (result && result.success) {
		   	     	BUI.Message.Alert('导入数据成功！', function(){
		   	     		importDialog.close();
		   	     		search.load();
		   	        }, 'success');
   	     		} else {
   	     			BUI.Message.Alert(result.error, function(){
		   	          //alert('确认');
		   	        }, 'error');
   	     		}
   	     	}
   	    }).render();
    	 
    	var importDialog = new Overlay.Dialog({
            title: '导入文件',
            width: 400,
            height: 200,
            contentId: 'importFileDiv',
            success:function () {
              //alert('确认');
              //this.close();
              uploader.upload();
            }
          });
    	importDialog.show();
    }

    function delItems(items){
      var ids = [];
      BUI.each(items,function(item){
        ids.push(item.id);
      });

      if(ids.length){
        BUI.Message.Confirm('确认要删除选中的记录么？',function(){
        	$.post('<%=contextPath %>/bizinfo/rzdbgs/editData.do?action=remove', {ids: ids}, function(ret) {
        		if(ret.success){ //删除成功
                	search.load();
                	BUI.Message.Alert('删除成功！');
                } else { //删除失败
                    BUI.Message.Alert('删除失败！');
                }
        	}, 'json');
        },'question');
      }
    }

    //监听事件，删除一条记录
    grid.on('cellclick', function(ev) {
    	var sender = $(ev.domTarget);
       var editor = editing.get('editor');
       var contain = false;
		if(sender.hasClass('btn-edit') && editor){
			contain = true;
			editing.edit(ev.record);
			setCompanySelectVisible(true);
			setInputValue("zdjgmc", ev.record.zdjgmc);
		}
       if (sender.hasClass('btn-view') && editor) {
       	contain = true;
     		var record = ev.record;
     		var view = {"view": true};
     		record.trigger_action = "view";
     		editing.showEditor(record);
     		setEditingEnable(editing, false);
     		setCompanySelectVisible(false);
     		setInputValue("zdjgmc", ev.record.zdjgmc);
     	}
       if (contain && grid.get('multipleSelect')){
		    return false;
		}
   });
    
 // select company 
    var selectStore = new Data.Store({
        url : '<%=contextPath %>/bizinfo/rzdbgs/baseCompanyList.do',
        autoLoad : true,
        //root: "rows",
        proxy: {
      		method: "post",
      		dataType : 'json'
        }
    });
      var select = new Select.Suggest({  
        render:'#companySelect',
        //valueField:'#hide',
        multipleSelect : false,
        name: 'company_name',
        store : selectStore,
        dataType : 'json',
        cacheable: true,
        url: '<%=contextPath %>/bizinfo/rzdbgs/selectCompany.do',
      });
      select.render();
      select.on('change', function(ev){
        //alert(ev.item.text);
        $("input[name='number']").val(ev.item.value);
        $("input[name='companyName']").val(ev.item.text);
      });
      
      function setInputValue(name, value) {
    	  var $input = $("input[name='"+name+"']");
    	  if ($input)
    		  $input.val(value ? value : "");
      }
      
      function setCompanySelectVisible(visible) {
    	  var $select = $("#selectCompanyDiv");
    	  var $greatOrg = $("input[name='zdjgmc']");
    	  if (visible) {
    		  $select.show();
    		  $select.find("input[name='company_name']").val();
    		  if ($greatOrg)
    			  $greatOrg.removeAttr("disabled");
    	  } else {
    		  $select.hide();
    		  if ($greatOrg)
    			  $greatOrg.attr("disabled", "disabled");
    	  }
      }
      
      var selectGeatOrg = new Select.Suggest({  
          render:'#zdjgmcSelect',
          //valueField:'#hide',
          multipleSelect : false,
          name: 'zdjgmc',
          dataType : 'json',
          cacheable: true,
          url: '<%=contextPath %>/bizinfo/rzdbgs/selectGeatOrg.do',
        });
      selectGeatOrg.render();
      
      
});

</script>

		
</body>
</html>